<template>
<div style="width: 400px;text-align:center;margin: 5px auto">
  <h1 STYLE="margin-bottom: 10px;">工程表编辑</h1>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="工程名称"  prop="projectName" >
      <el-input type="text" v-model="ruleForm.projectName" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="字符集"  prop="encode" >
      <el-input type="text" v-model="ruleForm.encode" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="是否选数据源" >
      <el-switch
        v-model="ruleForm.isDatasource"
        active-color="#13ce66"
        inactive-color="#ff4949"
        active-value="1"
        inactive-value="0">
      </el-switch>
    </el-form-item>
    <el-form-item label="自定义运行页面url" >
      <el-input type="text" v-model="ruleForm.customRunUrl" autocomplete="off" @keyup.native="autoSubmit($event)" ></el-input>
    </el-form-item>
    <el-form-item label="备注" >
      <el-input type="text" v-model="ruleForm.remark" autocomplete="off" @keyup.native="autoSubmit($event)" ></el-input>
    </el-form-item>
    <el-form-item >
      <div style="text-align:center">
        <el-button v-if="allModify==='1'" type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button v-if="allModify==='1'" @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="returnList">返回</el-button>
      </div>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
import request from "../utils/request";

export default {
  name: "edit",
  data(){
    const validateProjectName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入工程名称"));
      } else {
        callback();
      }
    };
     const validateEncode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入字符集"));
      } else {
        callback();
      }
    };
    const validateIsDatasource = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入是否选数据源"));
      } else {
        callback();
      }
    };

    return{
      ruleForm: {
        id: '',
        projectName: '',
        remark: '',
        customRunUrl: '',
        encode: '',
        isDatasource: '',
      },
      allModify:'0',
      rules: {
        projectName: [{ required: true,validator: validateProjectName, trigger: "blur" }],
        encode: [{ required: true,validator: validateEncode, trigger: "blur" }],
        isDatasource: [{ required: true,validator: validateIsDatasource, trigger: "blur" }],
      }
    }

  },
  methods: {
    autoSubmit: function (ev) {
      if (ev.code === "Enter" && this.allModify==='1') {
        this.submitForm('ruleForm');
      }
    },
    submitForm(formName) {
      let _this = this;
      const param = {
        id: _this.ruleForm.id,
        projectName: _this.ruleForm.projectName,
        remark: _this.ruleForm.remark,
        encode: _this.ruleForm.encode,
        isDatasource: _this.ruleForm.isDatasource,
        customRunUrl: _this.ruleForm.customRunUrl,
      };
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            method: "POST",
            url: "/api/Project/update",
            data: param
          })
            .then(res => {
              if (res.data.status == "1") {
                this.$message(res.data.message);
                _this.$router.push({path: "/project/list"});
              } else {
                this.$alert(res.data.message, {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
                return false;
              }
            })
            .catch(err => {
              debugger;
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadData(id){
      const  that = this;
      request({
        method: "POST",
        url: "/api/Project/getById?id="+id,
      })
        .then(res => {
          if (res.data.status == "1") {
            that.ruleForm.id =  res.data.bean.ID;
            that.ruleForm.projectName =  res.data.bean.PROJECT_NAME;
            that.ruleForm.remark =  res.data.bean.REMARK;
            that.ruleForm.encode =  res.data.bean.ENCODE;
            that.ruleForm.isDatasource =  res.data.bean.IS_DATASOURCE;
            that.ruleForm.customRunUrl =  res.data.bean.CUSTOM_RUN_URL;
            this.$message(res.data.message);
          } else {
            this.$alert(res.data.message, {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
            return false;
          }
        })
        .catch(err => {
          debugger;
          console.log(err);
        });
    },
    returnList(){
      this.$router.push({path: "/project/list"});
    }
  },
  mounted() {
    console.log("ID:"+this.$route.params.ID);
    this.ruleForm.id = this.$route.params.id;
    this.allModify = localStorage.allowProjectModify;
    this.loadData(this.ruleForm.id);
  }
}
</script>

<style scoped>

</style>
